package net.tngou.c4j;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.nodes.Document;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.model.InputData;
import net.tngou.c4j.model.Url;
import net.tngou.c4j.util.UrlRegex;
import net.tngou.c4j.util.UrlUtil;
import net.tngou.crawler.pojo.Data;
import net.tngou.crawler.pojo.Urldata;
import net.tngou.crawler.pojo.Weburl;

/**
 * 
 * @author tngou@tngou.net
 *
 */
public class Crawler {

	
	public Set<Url> getUrl(String url) {
		Document doc = CollectionGenerator.get(url);
		if(doc==null)return new HashSet<>();
		return 	UrlUtil.GetUrl(doc);
	}
	
	
	public Data getData(Weburl weburl ,Urldata urldata) {
		InputData input = change(weburl, urldata);
		return getData(weburl,input);
	}
	/**
	 * 
	 * @param input
	 * @return
	 */
	public Data getData(Weburl weburl,InputData input) {
		Data data = new Data();
		JSONObject json = new JSONObject();
		data.setUrldata(input.getId());
		Document doc = CollectionGenerator.get(input.getUrl());
		if(doc==null) return null;
		JSONObject keyvalue = input.getKey();
		JSONObject invalid = input.getInvalid();
		if(keyvalue==null)keyvalue=new JSONObject();
		if(invalid==null)invalid=new JSONObject();
		Set<String> keys=keyvalue.keySet();
		for (String e : keys) {
			String value = Fetcher.getValue(doc.body(), keyvalue.getString(e),invalid.getString(e));
			json.put(e, value);
		};
		data.setValue(json.toJSONString());
//		String equal=Fetcher.getValue(doc.body(), input.getUn());
		UrlRegex regex = new UrlRegex();
		String[] equal = regex.get(input.getUrl(), input.getUn());
		if(equal.length>0)
		data.setEqual(weburl.getId()+"_"+equal[0]);
		if(equal.length>1)
		{
			if(!NumberUtils.isNumber(equal[1]))equal[1]="0";
				data.setSeq(Integer.parseInt(equal[1]));
		}
		if(equal.length==1)
		{
			data.setSeq(0);
		}
		return data;
		
	}
	
	public InputData change(Weburl weburl ,Urldata urldata) {
		InputData inputData = new InputData();
		inputData.setId(urldata.getId());
		JSONObject key=new JSONObject();
		String keywavlue = weburl.getKey_value();
		key=(JSONObject) JSON.parse(keywavlue);
		JSONObject invalid=new JSONObject();
		String invalid_data = weburl.getInvalid_data();
		invalid=(JSONObject) JSON.parse(invalid_data);
		inputData.setKey(key);
		inputData.setInvalid(invalid);
		inputData.setUn(weburl.getUnique_value());
		inputData.setUrl(urldata.getUrl());
		return inputData;
	}
}
